package com.welsy.authoring.business
{
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.authoring.vo.course.Answer;
	import com.welsy.authoring.vo.course.Question;
	import com.welsy.authoring.vo.course.TestComponents;
	import com.welsy.authoring.vo.course.TestVO;
	import com.welsy.authoring.vo.course.pattern.ICourseIterator;
	import com.welsy.util.XMLUtils;
	
	import mx.collections.XMLListCollection;
	import mx.rpc.AbstractOperation;
	import mx.rpc.soap.WebService;
	import mx.rpc.soap.mxml.Operation;
	
	public class TestDao
	{
		
		[Bindable] private var model:CourseModelLocator = CourseModelLocator.getInstance();
		[Bindable] private var tc:TestComponents;  
		
		public function setTreeData(treeData:XMLListCollection):void {
      		treeData.removeAll();
      		tc = model.selectedTest;			  
			var qIterator:ICourseIterator = model.selectedTest.iterator()					
					
				while(qIterator.hasNext())
				{
					var q:Question;
		    		q = Question(qIterator.next());
		    		var questionNode:XML = q.getXML();			    		
		    			    		
		    		var aIterator:ICourseIterator = q.iterator();
		    		while (aIterator.hasNext())
		    		{
		    			var a:Answer = new Answer;
		    			a = Answer(aIterator.next());
		    			var answerNode:XML = a.getXML();			    			
		    			questionNode.appendChild(answerNode);
		    		}			    		
		    		treeData.addItem(questionNode);			    					    		
				} 	
      	}
      	
      	public function saveTestComponents(wsBlogAggr:WebService, test:TestVO, treeData:XMLListCollection):void {
	      		
	      		//save test components	      		
	      		tc.test = test;
	      		addNewQuestions(treeData);
	      		var xmlMessage:XML = getSaveTestMessage(tc);
				executeService(wsBlogAggr, "SaveTestComponents", xmlMessage);
	    } 
		
		private function addNewQuestions(questionList:XMLListCollection):void {
			
  			tc.removeAllItems();  
  			    		
      		for (var i:int = 0; i < questionList.length; i++) {
      			var myQuestion:Question = new Question;
      			myQuestion.id = questionList[i].@id;
				myQuestion.title = questionList[i].@label;
				myQuestion.weight = questionList[i].@weight;
				myQuestion.fileRepositoryID = questionList[i].@fileRepositoryID;
				myQuestion.fileName = questionList[i].@fileName;
				
				var myNestedXmlList:XMLList = questionList[i].child ("answer");
				if (myNestedXmlList != null) {
					for (var k:int = 0; k < myNestedXmlList.length(); k++) {
						if (myNestedXmlList[k] != null) {
							var myAnswerAttributes:Answer = new Answer;
							myAnswerAttributes.id = myNestedXmlList[k].@id;
							myAnswerAttributes.weight = myNestedXmlList[k].@weight;
							myAnswerAttributes.title = myNestedXmlList[k].@label;
							myAnswerAttributes.fileRepositoryID = myNestedXmlList[k].@fileRepositoryID;						
							myAnswerAttributes.fileName = myNestedXmlList[k].@fileName;						
							myQuestion.addItem(myAnswerAttributes);
						}
					}
				}					
				tc.addItem(myQuestion);	
      		} 
  		}
  		
  		//todo da togliere
  		private function getSaveTestMessage (testElement:TestComponents):XML {
		
			var utilsXml:XMLUtils = new XMLUtils;
							
			var sXml:String = "<SaveTestComponents xmlns='http://Welsy.ServiceContracts/2007/02'>";
      		sXml += "<ns1:SaveTestComponentsRequest xmlns:ns1='http://tempuri.org/TestDataTypes.xsd'>";
        	sXml += "<ns1:Test>";
        	
        	var test:TestVO = testElement.test;
        	
        	if (test.testId == null || test.testId == "")
        		test.testId = "0";
        			        		
        	sXml += "<ns1:TestID>" + test.testId + "</ns1:TestID>";	        	
        	sXml += "<ns1:Title>" + test.title + "</ns1:Title>";
        	sXml += "<ns1:QuestionSkipType>" + test.skipType + "</ns1:QuestionSkipType>";
        	
        	if (test.questionWeightType == null || test.questionWeightType == "")
        		test.questionWeightType = "0";

        	sXml += "<ns1:QuestionWeightType>" + test.questionWeightType + "</ns1:QuestionWeightType>";
        	sXml += "<ns1:PassingCondition>" + test.passingCondition + "</ns1:PassingCondition>";
        	sXml += "<ns1:DisplayCorrected>" + test.displayCorrected + "</ns1:DisplayCorrected>";
        	sXml += "<ns1:DisplayFeedback>" + test.displayFeedback + "</ns1:DisplayFeedback>";
        	sXml += "<ns1:DisplayCorrectAnswer>" + test.displayCorrectAnswere+ "</ns1:DisplayCorrectAnswer>";
        	sXml += "<ns1:TraceConsultationTime>" + test.traceConsultationTime + "</ns1:TraceConsultationTime>";
        	sXml += "<ns1:ConsultationTimeLimit>" + test.consultationTimeLimit + "</ns1:ConsultationTimeLimit>";
        	sXml += "</ns1:Test>";
        	sXml += "<ns1:Questions>";
        	
        	var qIterator:ICourseIterator = testElement.iterator();
        	var questionPosition:int = 1;
        	
        	while(qIterator.hasNext())
			{
				var q:Question;
		    	q = Question(qIterator.next());	        		        		
	    		
        		sXml += "<ns1:QuestionComponentsType>";
        		sXml += "<ns1:Question>";
        		sXml += "<ns1:QuestionID>" + q.id + "</ns1:QuestionID>";
				sXml += "<ns1:Title>" + utilsXml.Convert(q.title) + "</ns1:Title>";
				sXml += "<ns1:Position>" + String(questionPosition) + "</ns1:Position>";
				sXml += "<ns1:Weight>" + q.weight + "</ns1:Weight>";
				
        		if (q.fileRepositoryID == null || q.fileRepositoryID == "")
        			q.fileRepositoryID = "0";
				
				sXml += "<ns1:FileRepositoryID>" + q.fileRepositoryID + "</ns1:FileRepositoryID>"; 
				sXml += "<ns1:FileName>"+"todo fix file name"+"</ns1:FileName>"; 
        		sXml += "</ns1:Question>";
        		sXml += "<ns1:Answers>";
        		
        		
        		var answerPosition:int = 1;
        		var aIterator:ICourseIterator = q.iterator();
		    	while (aIterator.hasNext())
		    	{
		    		var a:Answer = new Answer;
		    		a = Answer(aIterator.next());	        			
        		    
        			sXml += "<ns1:AnswerType>";
        			sXml += "<ns1:AnswerID>" + a.id + "</ns1:AnswerID>";
			        sXml += "<ns1:Title>" + utilsXml.Convert(a.title) + "</ns1:Title>";
			        sXml += "<ns1:Position>" + answerPosition + "</ns1:Position>";
			        sXml += "<ns1:Weight>" + a.weight + "</ns1:Weight>";
			        sXml += "<ns1:FileRepositoryID>" + a.fileRepositoryID + "</ns1:FileRepositoryID>";
			        sXml += "<ns1:FileName>" + a.fileName + "</ns1:FileName>";
        			sXml += "</ns1:AnswerType>";
        			answerPosition++;
        		} 
        		sXml += "</ns1:Answers>";
        		sXml += "</ns1:QuestionComponentsType>";
        		questionPosition++;
        	}
        	
        	sXml += "</ns1:Questions>";
        	sXml += "</ns1:SaveTestComponentsRequest>";
    		sXml += "</SaveTestComponents>";
			
			return new XML (sXml);
		}
		
		
		//todo da togliere
        private function executeService (wsBlogAggr:WebService, opName:String, message:XML) : void
        {
        	var myAbstractOperation:AbstractOperation = wsBlogAggr.getOperation(opName);
        	var myOperation:Operation = Operation (myAbstractOperation);
        	myOperation.request = message;   
        	wsBlogAggr[opName].send ();
        }
		
	}
}